from flask import Flask, session, redirect, url_for,request
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
@app.route('/')
def index():
    if 'username' in session:
        username = session.get('username')
        return '用户名:' + username + '<br>' +"<b><a href = '/logout'>点击注销</a></b>"   #对应/logout
    return "未登录， <br><a href = '/login'></b>" +"点击登录</b></a>"   #对应/login
@app.route('/login', methods = ['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''
   <form action = "" method = "post">
      <p><input type ="text" name ="username"/></p>
      <p><input type ="submit" value ="登录"/></p>
   </form>
   '''

@app.route('/logout')
def logout():
   session.pop('username', None)
   return redirect(url_for('index'))

if __name__ == '__main__':
    app.run()


